# -*- coding: UTF-8 -*-
'''
@Project: stock
@File: ElasticSearch.py
@Notes: 
@Author: zhuyoucheng
@Date: 2021/4/1 0001 16:49
'''

import json
from App.Common.Urllib import Urllib
import config

class ElasticSearch(object):

    def __init__(self):
        self.url = config.ELASTICSEARCH_URL
    #查询
    def get(self, index='', type='', id=''):
        urls = self.url+'/{index}/{type}/{id}'.format(index=index, type=type, id=id)
        return Urllib().http_get(urls)

    #更新
    def put(self, index='', type='', id='', data={}):
        json_data = json.dumps(data)
        urls = self.url+'/{index}/{type}/{id}'.format(index=index, type=type, id=id)
        return Urllib().http_put(urls, json_data)

    #搜索
    def search(self, index='', type='', data={}):
        query_data = {"query": {"match": data}}
        json_data = json.dumps(query_data)
        if type:
            urls = self.url+'/{index}/{type}/_search'.format(index=index, type=type)
        else:
            urls = self.url + '/{index}/_search'.format(index=index)

        return Urllib().http_post(urls, json_data)
